﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProjectFinal {
	public partial class frmMedia : Form {
		private bool isNew;		//TRUE if we're creating a new Item, FALSE if editing one
		Item editMe = null;		//If Editing an item, this is a copy of the original item

		//Default behavior, create a new Item
		public frmMedia() {
			InitializeComponent();
			this.Text = "New Media";
			rb_adultBook.Checked = true;
			tb_author.Visible = true;
			lbl_author.Visible = true;
			isNew = true;
		}

		//Alternate behavior, edit an existing Item
		//Param:	Item to edit
		public frmMedia(Item i) {
			InitializeComponent();
			this.Text = "Edit Media";
			groupBox1.Enabled = false;			//You cannot change the media type--if it's that different, just make a new one
			tb_id.Enabled = !i.CheckedOut;  //You cannot change the ID either if the item is checked out
			tb_id.Text = i.ID;
			tb_title.Text = i.Title;
			if(i.getType() == MediaType.adultBook || i.getType() == MediaType.childBook) {
				tb_author.Text = ((Book)i).Author;
				tb_author.Visible = true;
				lbl_author.Visible = true;
				if(i.getType() == MediaType.adultBook)
					rb_adultBook.Checked = true;
				else
					rb_childBook.Checked = true;
			} else {
				tb_author.Visible = false;
				lbl_author.Visible = false;
				if(i.getType() == MediaType.DVD)
					rb_DVD.Checked = true;
				else 
					rb_video.Checked = true;
			}
			btn_addMedia.Text = "Update Media";
			isNew = false;
			editMe = i;
		}//frmMedia(Item)

		//Call this function when any of the Radio Buttons is changed
		private void group1_CheckedChanged(object sender, EventArgs e) {
			if(rb_adultBook.Checked || rb_childBook.Checked) {
				tb_author.Visible = true;
				lbl_author.Visible = true;
			} else {
				tb_author.Visible = false;
				lbl_author.Visible = false;
			}
		}

		private void btn_addMedia_Click(object sender, EventArgs e) {
			if(!isNew &&
					MessageBox.Show("Apply changes?", "Verify Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
				return;
			
			bool ok = true;	//assume all is well

			if(tb_id.Text.Trim().Equals("")) {  //Must have an ID number
				MessageBox.Show("Please enter an ID number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				ok = false;
			}
			if((isNew || (!editMe.CheckedOut && editMe.ID != tb_id.Text)) && 
					BusinessRules.getSpecificItem(tb_id.Text) != null) {
				MessageBox.Show("ID number: '" + tb_id.Text + "' is already in use.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				ok = false;
			}
			if(tb_title.Text.Trim().Equals("")) {  //Must have a title
				MessageBox.Show("Please enter a title.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				ok = false;
			}
			if((rb_adultBook.Checked || rb_childBook.Checked) && tb_author.Text.Trim().Equals("")) {  //Books must have an author
				MessageBox.Show("Please enter the book's author.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				ok = false;
			}
			if(!ok) return;

			//Everything is valid, make a new Item or update the existing one
			if(isNew) {
				if(rb_adultBook.Checked)
					BusinessRules.newBook(tb_title.Text, tb_author.Text, tb_id.Text, MediaType.adultBook);
				else if(rb_childBook.Checked)
					BusinessRules.newBook(tb_title.Text, tb_author.Text, tb_id.Text, MediaType.childBook);
				else if(rb_DVD.Checked)
					BusinessRules.newMedia(tb_title.Text, tb_id.Text, MediaType.DVD);
				else if(rb_video.Checked)
					BusinessRules.newMedia(tb_title.Text, tb_id.Text, MediaType.Video);
			} else {
				editMe.setTitle(tb_title.Text);
				editMe.setID(tb_id.Text);  //Provision for changing ID
				if(rb_adultBook.Checked || rb_childBook.Checked)
					((Book)editMe).Author = tb_author.Text;
			}
			BusinessRules.sortItems();
			this.Close();
		}

		private void btn_cancel_Click(object sender, EventArgs e) {
			this.Close();
		}
	}
}
